package org.ielse.frame.util;

import java.util.List;

import org.ielse.frame.R;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class DialogUtil {

	public static class ConfirmBuilder {
		private Context context;
		private Dialog dialog;
		private TextView txTitle;
		private Button btnConfirm, btnCancel;
		private View layoutContent;

		public static ConfirmBuilder init(Context context) {
			ConfirmBuilder cb = new ConfirmBuilder();
			cb.context = context;
			return cb;
		}

		public ConfirmBuilder create(View content, boolean canCancel, boolean shadow) {
			dialog = new Dialog(context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
			View view = LayoutInflater.from(context).inflate(R.layout.dialog_confirm, null);

			layoutContent = view.findViewById(R.id.lay_content);

			txTitle = (TextView) view.findViewById(R.id.tx_title);
			btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
			btnCancel = (Button) view.findViewById(R.id.btn_cancel);

			((FrameLayout) layoutContent).addView(content);

			btnConfirm.setVisibility(View.GONE);
			btnCancel.setVisibility(View.GONE);

			dialog.setContentView(view);
			dialog.setCanceledOnTouchOutside(canCancel);
			dialog.setCancelable(canCancel);
			return this;
		}

		public ConfirmBuilder setConfirmListener(OnClickListener confirmListener) {
			btnConfirm.setVisibility(View.VISIBLE);
			btnConfirm.setOnClickListener(confirmListener);
			return this;
		}

		public ConfirmBuilder setCancelListener(OnClickListener cancelListener) {
			btnCancel.setVisibility(View.VISIBLE);
			btnCancel.setOnClickListener(cancelListener);
			return this;
		}

		public ConfirmBuilder setTitleText(int resId) {
			txTitle.setText(resId);
			return this;
		}

		public ConfirmBuilder setTitleText(String text) {
			txTitle.setText(text);
			return this;
		}

		public ConfirmBuilder setConfirmText(int resId) {
			btnConfirm.setText(resId);
			return this;
		}

		public ConfirmBuilder setConfirmText(String text) {
			btnConfirm.setText(text);
			return this;
		}

		public ConfirmBuilder setCancelText(int resId) {
			btnCancel.setText(resId);
			return this;
		}

		public ConfirmBuilder setCancelText(String text) {
			btnCancel.setText(text);
			return this;
		}

		public Dialog show() {
			dialog.show();
			return dialog;
		}

		public Dialog complete() {
			return dialog;
		}

		public ConfirmBuilder createSimple(int textId, boolean canCancel, boolean shadow) {
			TextView txMessage = new TextView(context);
			txMessage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			final int spacing = BaseUtil.dipToPx(10);
			txMessage.setPadding(2 * spacing, spacing, 2 * spacing, spacing);
			txMessage.setTextColor(0xff5e5e5e);
			txMessage.setText(textId);
			return create(txMessage, canCancel, shadow);
		}
	}

	public static class BottomMenuBuidler {
		private Context context;
		private Dialog dialog;

		private Button btnCancel;
		private ViewGroup layContainer;

		public static BottomMenuBuidler init(Context context) {
			BottomMenuBuidler cb = new BottomMenuBuidler();
			cb.context = context;
			return cb;
		}

		public BottomMenuBuidler create(List<BottomMenu> menuList, boolean canCancel, boolean shadow) {
			dialog = new Dialog(context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
			Window window = dialog.getWindow();
			window.setWindowAnimations(R.style.Animation_Dialog_RisingFalling);

			window.getDecorView().setPadding(0, 0, 0, 0);
			WindowManager.LayoutParams lp = window.getAttributes();
			lp.width = WindowManager.LayoutParams.MATCH_PARENT;
			lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
			window.setAttributes(lp);
			window.setGravity(Gravity.BOTTOM);

			View view = LayoutInflater.from(context).inflate(R.layout.dialog_bottom_menu, null);

			btnCancel = (Button) view.findViewById(R.id.btn_cancel);
			layContainer = (ViewGroup) view.findViewById(R.id.lay_container);
			LayoutParams lpItem = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			LayoutParams lpDevider = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
			int spacing = BaseUtil.dipToPx(8);
			for (int i = 0; i < menuList.size(); i++) {
				BottomMenu bottomMenu = menuList.get(i);
				Button bbm = new Button(context);
				bbm.setLayoutParams(lpItem);
				int backgroundResId = R.drawable.selector_r3_ffffffff;
				if (menuList.size() > 1) {
					if (i == 0) {
						backgroundResId = R.drawable.selector_r3_top_ffffffff;
					}
					else if (i == menuList.size() - 1) {
						backgroundResId = R.drawable.selector_r3_bottom_ffffffff;
					}
				}
				bbm.setBackgroundResource(backgroundResId);
				bbm.setPadding(0, spacing, 0, spacing);
				bbm.setGravity(Gravity.CENTER);
				bbm.setText(bottomMenu.funName);
				bbm.setTextColor(0xff007aff);
				bbm.setTextSize(17);
				bbm.setOnClickListener(bottomMenu.listener);
				layContainer.addView(bbm);

				if (i != menuList.size() - 1) {
					View viewDevider = new View(context);
					viewDevider.setLayoutParams(lpDevider);
					viewDevider.setBackgroundColor(0xffcccccc);
					layContainer.addView(viewDevider);
				}
			}

			dialog.setContentView(view);
			dialog.setCanceledOnTouchOutside(canCancel);
			dialog.setCancelable(canCancel);
			return this;
		}

		public BottomMenuBuidler setCancelListener(OnClickListener cancelListener) {
			btnCancel.setVisibility(View.VISIBLE);
			btnCancel.setOnClickListener(cancelListener);
			return this;
		}

		public BottomMenuBuidler setCancelText(int resId) {
			btnCancel.setText(resId);
			return this;
		}

		public BottomMenuBuidler setCancelText(String text) {
			btnCancel.setText(text);
			return this;
		}

		public Dialog show() {
			dialog.show();
			return dialog;
		}

		public Dialog complete() {
			return dialog;
		}

		public static class BottomMenu {
			public String funName;
			public OnClickListener listener;

			public BottomMenu(String funName, OnClickListener listener) {
				this.funName = funName;
				this.listener = listener;
			}
		}
	}

}
